package com.xywl.escort.adapter.provider;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.annotation.ItemProviderTag;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.xywl.escort.R;
import com.xywl.escort.adapter.HomeRvAdapter;
import com.xywl.escort.adapter.MarketAdapter;
import com.xywl.escort.bean.MessageEvent;
import com.xywl.escort.entity.NormalMultipleEntity;

import org.greenrobot.eventbus.EventBus;

import static com.xywl.escort.common.Constant.EventType.EVENT_MAIN_TYPE;

@ItemProviderTag(
        viewType = HomeRvAdapter.TYPE_RECYCLE_GRID,
        layout = R.layout.view_market
)
public class RecycleGridItemProvider extends BaseItemProvider<NormalMultipleEntity, BaseViewHolder> {

    @Override
    public void convert(BaseViewHolder helper, final NormalMultipleEntity data, int position) {
        RecyclerView recyclerView = helper.getView(R.id.recycler_farm);
        GridLayoutManager manager = new GridLayoutManager(mContext, 2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        MarketAdapter adapter = new MarketAdapter(R.layout.item_market, data.marketData);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (null != data.marketData.get(position)) {
                    String spuId = data.marketData.get(position).spuId;
                    ARouter.getInstance().build("/xy/product/detail").withString("spuId", spuId).navigation();
                }
            }
        });

        TextView textView = helper.getView(R.id.tv_market_more);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new MessageEvent(EVENT_MAIN_TYPE, "market"));
            }
        });
    }

    @Override
    public void onClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
    }

    @Override
    public boolean onLongClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
        return false;
    }
}
